package com.qiangesoft.miniapp.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义sql字段填充器，自动填充创建修改相关字段
 *
 * @author qiangesoft
 * @date 2024-04-11
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("createUser", StpUtil.getLoginIdAsLong(), metaObject);
        setFieldValByName("createTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateUser", StpUtil.getLoginIdAsLong(), metaObject);
        setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}

